package com.sskj.market.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.support.constraint.Group;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.common.util.MD5Util;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.NumberUtil;
import com.sskj.market.R;
import com.sskj.market.R2;
import com.sskj.market.bean.OrderDetailsBean;
import com.sskj.market.component.DaggerUserDataComponent;
import com.sskj.market.http.HttpConfig;
import com.sskj.market.presenter.OrderDetailsPresenter;
import com.sskj.market.util.GlideUtils;

import java.util.Objects;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author budou
 */
public class OrderDetailsActivity extends BaseActivity<OrderDetailsPresenter> {


    @BindView(R2.id.tvReturn)
    TextView tvReturn;
    @BindView(R2.id.view_address_order_details_market)
    View viewAddressOrderDetailsMarket;
    @BindView(R2.id.group_address_info_order_market)
    Group groupAddressInfoOrderMarket;
    @BindView(R2.id.iv_item_map_order_details_market)
    ImageView ivItemMapOrderDetailsMarket;
    @BindView(R2.id.tv_name_address_order_details_market)
    TextView tvNameAddressOrderDetailsMarket;
    @BindView(R2.id.tv_phone_address_order_details_market)
    TextView tvPhoneAddressOrderDetailsMarket;
    @BindView(R2.id.tv_info_address_order_details_market)
    TextView tvInfoAddressOrderDetailsMarket;
    @BindView(R2.id.tv_type_order_details_market)
    TextView tvTypeOrderDetailsMarket;
    @BindView(R2.id.view_goods_order_details_market)
    View viewGoodsOrderDetailsMarket;
    @BindView(R2.id.tv_stores_name_order_details_market)
    TextView tvStoresNameOrderDetailsMarket;
    @BindView(R2.id.iv_goods_name_order_details_market)
    ImageView ivGoodsNameOrderDetailsMarket;
    @BindView(R2.id.tv_goods_name_order_details_market)
    TextView tvGoodsNameOrderDetailsMarket;
    @BindView(R2.id.tv_goods_price_order_details_market)
    TextView tvGoodsPriceOrderDetailsMarket;
    @BindView(R2.id.iv_goods_count_order_details_market)
    TextView ivGoodsCountOrderDetailsMarket;
    @BindView(R2.id.view_goods_info_order_details_market)
    View viewGoodsInfoOrderDetailsMarket;
    @BindView(R2.id.tv_goods_express_price_title_order_details_market)
    TextView tvGoodsExpressPriceTitleOrderDetailsMarket;
    @BindView(R2.id.tv_goods_express_company_title_order_details_market)
    TextView tvGoodsExpressCompanyTitleOrderDetailsMarket;
    @BindView(R2.id.tv_goods_remark_title_order_details_market)
    TextView tvGoodsRemarkTitleOrderDetailsMarket;
    @BindView(R2.id.tv_goods_express_price_order_details_market)
    TextView tvGoodsExpressPriceOrderDetailsMarket;
    @BindView(R2.id.tv_goods_express_info_order_details_market)
    TextView tvGoodsExpressInfoOrderDetailsMarket;
    @BindView(R2.id.et_goods_remark_order_details_market)
    EditText etGoodsRemarkOrderDetailsMarket;
    @BindView(R2.id.group_express_info)
    Group groupExpressInfo;
    @BindView(R2.id.tv_goods_all_price_order_details_market)
    TextView tvGoodsAllPriceOrderDetailsMarket;
    @BindView(R2.id.tv_goods_all_count_order_details_market)
    TextView tvGoodsAllCountOrderDetailsMarket;
    @BindView(R2.id.btn_ensure_order_details_market)
    Button btnEnsureOrderDetailsMarket;
    @BindView(R2.id.btn_clear_order_details_market)
    Button btnClearOrderDetailsMarket;
    @BindView(R2.id.tv_order_id_order_details_market)
    TextView tvOrderId;

    private int orderId;
    private int type;
    private String allPrice, sn;
    @Inject
    UserViewModel userViewModel;
    UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.market_activity_order_details;
    }

    @Override
    public OrderDetailsPresenter getPresenter() {
        return new OrderDetailsPresenter();
    }

    @Override
    protected void initData() {

    }


    @Override
    protected void initView() {
        super.initView();

        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });

        setTitle(App.INSTANCE.getString(R.string.market_orderDetailsActivity1));
        tvReturn.setVisibility(View.VISIBLE);
        Bundle bundle = getIntent().getExtras();
        assert bundle != null;
        orderId = bundle.getInt("orderId");
        mPresenter.getOrderInfo(orderId + "", mPresenter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ClickUtil.click(btnClearOrderDetailsMarket, () -> {
            if (!AppCircleCheckUtil.checkLogin(this, userData)) {
                return;
            }
            mPresenter.cancelOrder(orderId + "", mPresenter);
        });
        ClickUtil.click(btnEnsureOrderDetailsMarket, () -> {
            if (!AppCircleCheckUtil.checkLogin(this, userData)) {
                return;
            }
            switch (type) {
                case 1:
                    showPayDialog();
                    break;
                case 3:
                    mPresenter.ensureOrder(orderId + "", mPresenter);
                    break;
                case 0:
                    mPresenter.deleteOrder(orderId + "", mPresenter);
                    break;
                default:
                    break;
            }

        });
    }


    @SuppressLint("SetTextI18n")
    private void showPayDialog() {
        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(this)
                .inflate(R.layout.market_dialog_pay, null);
        Dialog dialog = new Dialog(this, R.style.market_bottom_dialog);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(true);
        Button btnEnsure = view.findViewById(R.id.btn_pay_ensure_dialog);
        TextView tvPrice = view.findViewById(R.id.tv_pay_price_dialog);
        EditText etPay = view.findViewById(R.id.et_pay_input_dialog);
        tvPrice.setText(App.INSTANCE.getString(R.string.market_orderItemFragment12) + NumberUtil.keep(allPrice,2) + "LAC");

        ClickUtil.click(btnEnsure, () -> {
            if (TextUtils.isEmpty(etPay.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.market_orderItemFragment13));
                return;
            }
            mPresenter.payOrder(sn, MD5Util.getMd5Value(etPay.getText().toString()), mPresenter);
            dialog.dismiss();
        });
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.width = getWindowManager().getDefaultDisplay().getWidth();
        view.setLayoutParams(layoutParams);
        Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.BOTTOM);
        dialog.getWindow().setWindowAnimations(R.style.market_pop_show_style);
        dialog.show();


    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(this, OrderListActivity.class));
    }


    /**
     * 请求详情返回值
     *
     * @param bean 返回详情数据
     */
    @SuppressLint("SetTextI18n")
    public void getDetailsSuccess(OrderDetailsBean bean) {
        allPrice = String.valueOf(Double.parseDouble(bean.getData().getTotal_price()) + Double.parseDouble(bean.getData().getShipping_money()));
        sn = bean.getData().getSn();
        if (bean.getData().getProduct().getPics().size() > 0) {
            GlideUtils.Show(bean.getData().getProduct().getPics().get(0), ivGoodsNameOrderDetailsMarket);
        }
        tvNameAddressOrderDetailsMarket.setText(bean.getData().getAddress_name());
        tvPhoneAddressOrderDetailsMarket.setText(bean.getData().getAddress_phone());
        tvInfoAddressOrderDetailsMarket.setText(bean.getData().getAddress_detail());
        tvOrderId.setText(App.INSTANCE.getString(R.string.market_orderItemFragment1) + bean.getData().getSn());
        type = bean.getData().getStatus();
        tvGoodsNameOrderDetailsMarket.setText(bean.getData().getProduct().getName());
        tvGoodsPriceOrderDetailsMarket.setText(bean.getData().getProduct().getPrice() + " LAC");
        tvGoodsExpressPriceOrderDetailsMarket.setText(bean.getData().getShipping_money() + "LAC");
        ivGoodsCountOrderDetailsMarket.setText("×" + bean.getData().getTotal_num());
        tvGoodsAllCountOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_submitOrderActivity2) + bean.getData().getTotal_num() + App.INSTANCE.getString(R.string.market_submitOrderActivity3));
        etGoodsRemarkOrderDetailsMarket.setText(bean.getData().getRemark());
        tvGoodsAllPriceOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_submitOrderActivity4) + NumberUtil.keep(allPrice,2) + "LAC");

        switch (bean.getData().getStatus()) {
            case 1:
                //待付款
                tvTypeOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderListActivity2));
                groupExpressInfo.setVisibility(View.GONE);
                btnClearOrderDetailsMarket.setVisibility(View.VISIBLE);
                btnEnsureOrderDetailsMarket.setVisibility(View.VISIBLE);
                btnEnsureOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderDetailsActivity9));
                btnClearOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderItemFragment5));
                groupExpressInfo.setVisibility(View.GONE);
                break;
            case 2:
                //待发货
                tvTypeOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderListActivity3));
                btnEnsureOrderDetailsMarket.setVisibility(View.GONE);
                btnClearOrderDetailsMarket.setVisibility(View.GONE);
                groupExpressInfo.setVisibility(View.GONE);
                break;
            case 3:
                //待收货
                tvTypeOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderListActivity4));
                btnClearOrderDetailsMarket.setVisibility(View.GONE);
                btnEnsureOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderItemFragment8));
                groupExpressInfo.setVisibility(View.VISIBLE);
                tvGoodsExpressInfoOrderDetailsMarket.setText(bean.getData().getExpress_info());
                break;
            case 4:
                //完成订单
                tvTypeOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderListActivity5));
                btnEnsureOrderDetailsMarket.setVisibility(View.GONE);
                btnClearOrderDetailsMarket.setVisibility(View.GONE);
                groupExpressInfo.setVisibility(View.GONE);
                break;
            case 0:
                tvTypeOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderItemFragment10));
                btnEnsureOrderDetailsMarket.setVisibility(View.VISIBLE);
                btnClearOrderDetailsMarket.setVisibility(View.GONE);
                btnEnsureOrderDetailsMarket.setText(App.INSTANCE.getString(R.string.market_orderItemFragment11));
                groupExpressInfo.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

    /**
     * 取消成功
     */
    public void cancelSuccess() {
        mPresenter.getOrderInfo(orderId + "", mPresenter);
        ToastUtil.showShort(App.INSTANCE.getString(R.string.market_cancel_success));
    }

    /**
     * 删除成功
     */
    public void deleteSuccess() {
        finish();
        ToastUtil.showShort(App.INSTANCE.getString(R.string.market_delete_success));
    }

    /**
     * 确认收货成功
     */
    public void ensureSuccess() {
        mPresenter.getOrderInfo(orderId + "", mPresenter);
        ToastUtil.showShort(App.INSTANCE.getString(R.string.market_ensure_success));
    }

    /**
     * 支付成功
     */
    public void paySuccess() {
        mPresenter.getOrderInfo(orderId + "", mPresenter);
        ToastUtil.showShort(App.INSTANCE.getString(R.string.market_pay_success));
    }


}
